package com.liang.blog.reception.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.liang.blog.Enum.article.ArticleAuditEnum;
import com.liang.blog.common.exception.BlogException;
import com.liang.blog.common.result.ResultCodeEnum;
import com.liang.blog.model.BlogArticle;
import com.liang.blog.reception.mapper.BlogArticleMapper;
import com.liang.blog.reception.service.BlogArticleService;
import com.liang.blog.security.utils.BlogSecurityUtil;
import com.liang.blog.vo.article.ArticleInfoVo;
import com.liang.blog.vo.article.BlobCommentsVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 *
 * @Author：YanWenLiang
 * @Date:2023-09-20-8:52
 */
@Service
public class BlogArticleServiceImpl implements BlogArticleService {

    @Autowired
    private BlogArticleMapper blogArticleMapper;

    /**
     * 通过id获取文章信息
     * @param id
     * @return
     */
    @Override
    public ArticleInfoVo getArticleById(Long id) {
        blogArticleMapper.updateArticlePageView(id);
        return blogArticleMapper.getArticleById(id);
    }

    /**
     * 添加文章
     * @param articleInfoVo
     * @return
     */
    @Override
    public Boolean addArticle(ArticleInfoVo articleInfoVo) {
        if (ObjectUtil.isEmpty(articleInfoVo)){
            throw new BlogException(ResultCodeEnum.PARAM_ERROR);
        }

        Long userId = BlogSecurityUtil.getUserId();
        if (userId == null) {
            throw new BlogException("请先前往登入", ResultCodeEnum.LOGIN_AUTH.getCode());
        }

        articleInfoVo.setUserId(userId);
        articleInfoVo.setCreateTime(new Date());
        articleInfoVo.setAuditStatus(ArticleAuditEnum.NOT_AUDIT.getCode());
        articleInfoVo.setPageView(0);
        return blogArticleMapper.addArticle(articleInfoVo);
    }

    /**
     * 获取文章列表
     * @param articleInfoVo
     * @return
     */
    @Override
    public List<ArticleInfoVo> getArticleList(ArticleInfoVo articleInfoVo) {
        return blogArticleMapper.getArticleList(articleInfoVo);
    }

    /**
     * 添加评论
     * @param blobCommentsVO
     * @return
     */
    @Override
    public Boolean addArticleComments(BlobCommentsVO blobCommentsVO) {
        if (!ObjectUtil.isNotEmpty(blobCommentsVO)){
            throw new BlogException(ResultCodeEnum.PARAM_ERROR);
        }

        Long userId = BlogSecurityUtil.getUserId();
        if (userId == null) {
            throw new BlogException("请先前往登入", ResultCodeEnum.LOGIN_AUTH.getCode());
        }

        blobCommentsVO.setUserId(userId);
        blobCommentsVO.setCreateTime(new Date());

        return blogArticleMapper.addArticleComments(blobCommentsVO) > 1;
    }

    /**
     * 根据文章id获取相关评论
     * @param articleId
     * @return
     */
    @Override
    public Map<String, Object> getArticleComments(Long articleId) {
        List<BlobCommentsVO> blobCommentsVOList = blogArticleMapper.getArticleComments(articleId);
        Map<String, Object> resultMap = new HashMap<>();
        Map<Long, BlobCommentsVO> map = new HashMap<>();
        List<BlobCommentsVO> result = new ArrayList<>();

        for (BlobCommentsVO commentsVO : blobCommentsVOList) {
            if (!map.containsKey(commentsVO.getId())){
                map.put(commentsVO.getId(), commentsVO);
            }

            if (0 == commentsVO.getParentId()) {
                result.add(map.get(commentsVO.getId()));
                continue;
            }

            BlobCommentsVO rootBlobCommentsVO = map.get(commentsVO.getParentId());
            if (!ObjectUtil.isEmpty(rootBlobCommentsVO)) {
                rootBlobCommentsVO.getBlobCommentsVOList().add(commentsVO);
            }

        }

        resultMap.put("count", blobCommentsVOList.size());
        resultMap.put("result", result);
        return resultMap;
    }

    @Override
    public Boolean deleteByIds(List<String> idList) {
        if (CollectionUtils.isEmpty(idList)) {
            throw new BlogException(ResultCodeEnum.PARAM_ERROR);
        }
        return blogArticleMapper.deleteByIds(idList) > 0;
    }
}
